/**
 * Created by 000188 on 2016/6/27.
 */
import React from 'react';
let Logo = require('logo');
let Turntable = require('turntable');
let Btn = require('btn');
let Content = require('content');
import Ajax from 'superagent';
let EndMsg = require('end-msg');
let loadImg = require('load-img');

var Luck = React.createClass({
    render(){
        return <div>
            <Logo />
            <Turntable rotate={this.rotate}/>
        </div>
    },
    toScene(){
        require(['rule'], Mod=> {
            Content.render(<Mod/>)
        })
    },
    rotate(T){
        if (this.start) {
            return
        }
        this.start = true;

        require(['config'], Config=> {
            Ajax.post( Config.api.user)
                .set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
                .send({
                    type: 6
                })
                .end((err, res)=> {
                    if (err) return;
                    var data = JSON.parse(res.text);
                    switch (data.SuccessCode) {
                        case '0':
                            T.rotate(data.DataItem);
                            setTimeout(()=> {
                                Content.render(<EndMsg imgUrl={loadImg.imgs[data.DataItem]} />);
                            }, 6500);
                            break;
                        case '1':
                            Content.render(<EndMsg imgUrl={loadImg.states[0]} />);
                            break;
                        case '-2':
                            Content.render(<EndMsg imgUrl={loadImg.states[1]} />);
                            break;
                        case '-3':
                            Content.render(<EndMsg imgUrl={loadImg.states[2]} />);
                            break;
                    }
                })
        })
    },
    start: false
});


module.exports = Luck;

